import apis from "../services/addMessageApi";

export default {
  namespace: "saveMessage",
  state: {
    saveError: -1,
    saveErrMsg: "",
    key: {},
    subError: -1,
    subErrMsg: "",
    detailMessage: {},
    detailKey: {},
    loading: false,
    messageTypeList: []
  },
  subscriptions: {
    // 监听地址工作台
    setup({ dispatch, history }) {
      history.listen(location => {
        if (
          location.pathname ===
          "/messageCenter/messageManager/messageSource/messageAdd"
        ) {
          if (location.search.indexOf("key") >= 0) {
            const key = JSON.parse(
              window.decodeURIComponent(location.search).substring(5)
            );
            dispatch({ type: "put/putkey", payload: key });
            // if (key.operate === "mod") {
            //   dispatch({
            //     type: "detailPlan",
            //     payload: { planId: key.planId }
            //   });
            // }
          }
        }
        if (location.pathname.includes("messageDetail")) {
          if (location.search.indexOf("key") >= 0) {
            const key = JSON.parse(
              window.decodeURIComponent(location.search).substring(5)
            );
            dispatch({ type: "put/putDetailKey", payload: key });
            // if (key.operate === "mod") {
            //   dispatch({
            //     type: "detailPlan",
            //     payload: { planId: key.planId }
            //   });
            // }
          }
        }
        if (
          location.pathname === "/messageCenter/messageManager/messageSource"
        ) {
          dispatch({ type: "put/detail", payload: {} });
          // if (key.operate === "mod") {
          //   dispatch({
          //     type: "detailPlan",
          //     payload: { planId: key.planId }
          //   });
          // }
        }
      });
    }
  },
  effects: {
    // 推送数量趋势图
    *saveMessage({ payload }, { call, put }) {
      const res = yield call(apis.saveMessage, payload);
      const { errCode = "-1", errMsg } = res;
      yield put({
        type: "put/saveError",
        payload: errCode
      });
      yield put({
        type: "put/saveerrMsg",
        payload: errMsg
      });
    },
    *saveSendMessage({ payload }, { call }) {
      return yield call(apis.saveSendMessage, payload);
    },
    *editMessage({ payload }, { call }) {
      return yield call(apis.editMessage, payload);
    },
    *submitMessage({ payload }, { call, put }) {
      const res = yield call(apis.submitMessage, payload);
      const { errCode = "-1", errMsg } = res;
      yield put({
        type: "put/subError",
        payload: errCode
      });
      yield put({
        type: "put/suberrMsg",
        payload: errMsg
      });
    },
    *getDetail({ payload }, { call, put }) {
      const res = yield call(apis.getDetail, payload);
      const { datas = {} } = res;
      yield put({
        type: "put/loading",
        payload: true
      });
      yield put({
        type: "put/detail",
        payload: datas
      });
      yield put({
        type: "put/loading",
        payload: false
      });
    },
    *listMessageTypes({ payload }, { call, put }) {
      const res = yield call(apis.listMessageTypes, payload);
      const { datas = {} } = res;
      yield put({
        type: "put/listMessageTypes",
        payload: datas
      });
    }
  },
  reducers: {
    "put/saveError": (state, { payload }) => ({
      ...state,
      saveError: payload
    }),
    "put/saveerrMsg": (state, { payload }) => ({
      ...state,
      saveErrMsg: payload
    }),
    "put/putkey": (state, { payload }) => ({
      ...state,
      key: payload
    }),
    "put/subError": (state, { payload }) => ({
      ...state,
      subError: payload
    }),
    "put/suberrMsg": (state, { payload }) => ({
      ...state,
      subErrMsg: payload
    }),
    "put/detail": (state, { payload }) => ({
      ...state,
      detailMessage: payload
    }),
    "put/putDetailKey": (state, { payload }) => ({
      ...state,
      detailKey: payload
    }),
    "put/loading": (state, { payload }) => ({
      ...state,
      loading: payload
    }),
    "put/listMessageTypes": (state, { payload }) => ({
      ...state,
      messageTypeList: payload
    })
  }
};
